package com.hxd.otc.module.my.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.common.enums.TradeType;
import com.hxd.otc.dialog.AdvertFiatPopupWindow;
import com.hxd.otc.dialog.MultipleBlocksDialog;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.my.bean.AdRelatedAmountBean;
import com.hxd.otc.module.my.bean.AdvertCoinPriceBean;
import com.hxd.otc.module.my.bean.AdvertCoinTypeBean;
import com.hxd.otc.module.my.bean.AdvertPreBean;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.contract.PublishAdvertisementContract;
import com.hxd.otc.module.my.presenter.PublishAdvertisementPresenter;
import com.hxd.otc.other.CustomBounceTopEnter;
import com.hxd.otc.other.SlideTopExit;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.LimitNumberTextWatcher;
import com.hxd.otc.widget.TitleBar;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作  者 :  李宗源
 * 时  间 :  2018/10/18 14:18
 * 描  述 :  ${发布广告}
 */
public class PublishAdvertisementActivity extends BaseActivity<PublishAdvertisementPresenter> implements PublishAdvertisementContract.View {

    @BindView(R.id.rb_sell)
    RadioButton mRbSell;
    @BindView(R.id.rb_buy)
    RadioButton mRbBuy;
    @BindView(R.id.rg_deal_type)
    RadioGroup mRgDealType;
    @BindView(R.id.tv_coin)
    TextView mTvCoin;
    @BindView(R.id.tv_fiats)
    TextView mTvFiats;
    @BindView(R.id.et_unit_price)
    EditText mEtUnitPrice;
    @BindView(R.id.tv_unit_price_unit)
    TextView mTvUnitPriceUnit;
    @BindView(R.id.et_nums)
    EditText mEtNums;
    @BindView(R.id.tv_unit_nums)
    TextView mTvUnitNums;
    @BindView(R.id.tv_fee)
    TextView mTvFee;
    @BindView(R.id.flex_box_layout)
    FlexboxLayout mFlexboxLayout;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.scroll_view)
    NestedScrollView mNestedScrollView;
    @BindView(R.id.cb_floating_pricing)
    CheckBox mCbFloatingPricing;
    @BindView(R.id.tv_floating_rate_label)
    TextView mTvFloatingRateLabel;
    @BindView(R.id.tv_tracking_platform)
    TextView mTvTrackingPlatform;
    @BindView(R.id.fl_tracking_platform_guide)
    FrameLayout mFlTrackingPlatformGuide;
    @BindView(R.id.fl_floating_rate_guide)
    FrameLayout mFlFloatingRateGuide;
    @BindView(R.id.fl_low_price_guide)
    FrameLayout mFlLowPriceGuide;
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.cl_variable_pricing)
    ConstraintLayout mClVariablePricing;
    @BindView(R.id.cb_floating_rate_mode)
    CheckBox mCbFloatingRateMode;
    @BindView(R.id.et_floating_rate)
    EditText mEtFloatingRate;
    @BindView(R.id.tv_valuation)
    TextView mTvValuation;
    @BindView(R.id.et_low_price)
    EditText mEtLowPrice;
    @BindView(R.id.tv_quota_unit)
    TextView mTvQuotaUnit;
    @BindView(R.id.et_max_quota)
    EditText mEtMaxQuota;
    @BindView(R.id.et_min_quota)
    EditText mEtMinQuota;
    @BindView(R.id.tv_low_price_desc)
    TextView mTvLowPriceDesc;
    @BindView(R.id.tv_payment_methods_label)
    TextView mTvPaymentMethodsLabel;
    @BindView(R.id.tv_explain)
    TextView mTvExplain;
    @BindView(R.id.tv_weichat_fee)
    TextView tvWeichatFee;
    @BindView(R.id.tv_alipay_fee)
    TextView tvAlipayFee;
    @BindView(R.id.tv_bank_card_fee)
    TextView tvBankCardFee;

    private static final int REQUEST_CODE_SET_COLLECTION_MODE = 2002;

    private AdvertFiatPopupWindow mSelectFiatPopupWindow;
    private AdvertFiatPopupWindow mSelectCoinPopupWindow;
    private AdvertFiatPopupWindow mSelectTrackingPlatformPopupWindow;

    public static final String EXTRA_COIN_TYPES = "coinTypes";
    public static final String EXTRA_TRADE_MODE = "tradeModes";
    private MultipleBlocksDialog mMultipleDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_publish_advertisement;
    }

    @Override
    protected PublishAdvertisementPresenter createPresenter() {
        return new PublishAdvertisementPresenter(this);
    }

    @Override
    protected void initView() {
        mEtUnitPrice.setEnabled(UserHelper.isNonInductivePaySilver());//当为无感支付广告商时不可编辑单价
        mTvExplain.setVisibility(UserHelper.isNonInductivePaySilver() ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void initData() {
        Bundle bundle = getBundle();
        //根据使用场景进行不同处理：
        //场景1：从我的广告页面进入，此时有预准备信息
        //场景2：从交易页面进入，没有预准备信息
        if (bundle != null) {
            ArrayList<BankCardBean> bankCardBeans = (ArrayList<BankCardBean>) bundle.getSerializable(EXTRA_TRADE_MODE);
            if (bankCardBeans == null) {
                mPresenter.requestBankCardList(false);
            } else {
                mPresenter.setTradeModes(bankCardBeans);//当有收款方式信息，更新收款方式
            }
            ArrayList<AdvertCoinTypeBean> coinTypes = (ArrayList<AdvertCoinTypeBean>) bundle.getSerializable(EXTRA_COIN_TYPES);
            if (coinTypes == null) {
                mPresenter.requestCoinTypeInfo(false);
            } else if (coinTypes.size() > 0) {
                mPresenter.setCoinTypes(coinTypes);//更新币种信息
                updateCoin(mPresenter.getCoins().get(0));//更新选中的币种
            }
        } else {
            mPresenter.requestCoinTypeInfo(false);
            mPresenter.requestBankCardList(false);
        }
    }

    @Override
    protected void initEvent() {
        if (!UserHelper.isNonInductivePaySilver()) {
            mEtUnitPrice.addTextChangedListener(new LimitNumberTextWatcher(mEtUnitPrice, 9, 2));//单价输入限制监听
        }
        mEtNums.addTextChangedListener(new LimitNumberTextWatcher(mEtNums, 9, 6) {//数量输入限制监听
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                super.onTextChanged(s, start, before, count);
//                updateFee();//更新手续费
            }
        });
        mRgDealType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//交易方式改变监听
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                updateLowPriceDesc();//更新撤单价格文案
//                updateFee();//更新手续费
                updateAllFee();//更新手续费
                mTvPaymentMethodsLabel.setText(checkedId == R.id.rb_buy ? R.string.payment_method : R.string.my_trading_mode);//更新收付款方式提示文案
                mEtUnitPrice.setHint(mCbFloatingPricing.isChecked() ? getString(R.string.please_set_the_floating_price) : getNorSinglePriceDesc());//单价输入提示文案更新
            }
        });
        mCbFloatingPricing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {//浮动平台更新监听
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mClVariablePricing.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                mEtUnitPrice.setEnabled(!isChecked);
                mTvUnitPriceUnit.setEnabled(!isChecked);
                mEtUnitPrice.setText("");
                mEtUnitPrice.setHint(isChecked ? getString(R.string.please_set_the_floating_price) : getNorSinglePriceDesc());
            }
        });
        mCbFloatingRateMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCbFloatingRateMode.setText(isChecked ? R.string.floating_upward : R.string.floating_downward);
                updateValuation();
            }
        });

        mEtFloatingRate.addTextChangedListener(new LimitNumberTextWatcher(mEtFloatingRate, 2, 2) {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                super.onTextChanged(s, start, before, count);
                String floatRateStr = mEtFloatingRate.getText().toString().trim();
                if (TextUtils.isEmpty(floatRateStr)) {
                    mEtFloatingRate.setText("0");
                    return;
                }
                updateValuation();
            }
        });
        mEtMaxQuota.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                mTvQuotaUnit.setSelected(hasFocus);
            }
        });

        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {
            }

            @Override
            public void onRight(View v) {

                Bundle bundle = new Bundle();
                bundle.putBoolean(PublishAdvertHelpActivity.isNoSense, UserHelper.isNonInductivePaySilver());
                $startActivity(PublishAdvertHelpActivity.class, bundle);
            }
        });
    }

    /**
     * 更新撤单价格文案
     */
    private void updateLowPriceDesc() {
        mTvLowPriceDesc.setText(mRgDealType.getCheckedRadioButtonId() == R.id.rb_buy
                ? R.string.when_the_market_price_is_higher_than_this_the_system_will_automatically_withdraw_this_advertisement
                : R.string.when_the_market_price_is_lower_than_this_the_system_will_automatically_withdraw_this_advertisement);
    }

    /**
     * 更新手续费
     */
    private void updateFee() {
     /*   String numsStr = mEtNums.getText().toString().trim();
        if (TextUtils.isEmpty(numsStr)) {
            mTvFee.setText("");
            mTvFee.setVisibility(View.GONE);
            return;
        }
        BigDecimal numsBigDecimal = new BigDecimal(numsStr);
        boolean isBuy = mRgDealType.getCheckedRadioButtonId() == R.id.rb_buy;

        AdvertCoinTypeBean selCoinTypeBeans = mPresenter.getSelCoinTypeBeans();
        if (TextUtils.isEmpty(numsStr) || selCoinTypeBeans == null) {
            mTvFee.setText("");
            mTvFee.setVisibility(View.GONE);
            return;
        }

        AdvertFeeEnum advertFeeEnum = selCoinTypeBeans.getAdvertFeeEnum();
        if (AdvertFeeEnum.NONE.equals(advertFeeEnum)) {//不收手续费
            mTvFee.setText("");
            mTvFee.setVisibility(View.GONE);
            return;
        }

        mTvFee.setVisibility(View.VISIBLE);
        if (AdvertFeeEnum.STATIC.equals(advertFeeEnum)) {
            mTvFee.setText(getString(R.string.format_advert_fee,
                    isBuy ? selCoinTypeBeans.getBuyFee() : selCoinTypeBeans.getSellFee(),
                    selCoinTypeBeans.getSymbol()));
            return;
        }

        BigDecimal feeBigDecimal = new BigDecimal(isBuy ? selCoinTypeBeans.getBuyFee() : selCoinTypeBeans.getSellFee());

        double fee = numsBigDecimal.multiply(feeBigDecimal).doubleValue();

        mTvFee.setText(getString(R.string.format_advert_fee, StringUtil.formatNumber(fee, 6, false), selCoinTypeBeans.getSymbol()));
*/
    }


    /**
     * 更新手续费
     */
    private void updateAllFee() {

        LogUtils.e("======updateAllFee=================");
        boolean isBuy = mRgDealType.getCheckedRadioButtonId() == R.id.rb_buy;

        AdvertCoinTypeBean selCoinTypeBeans = mPresenter.getSelCoinTypeBeans();
        if (selCoinTypeBeans == null) {
            setViewFeeGone();
            return;
        }
        if (UserHelper.isNonInductivePaySilver()) {//取无感 otcEasyPayFees
            setOtcEasePayFee(isBuy);
        } else {//普通 otPayFees
            setOtcPayFee(isBuy);
        }
    }

    private void setOtcEasePayFee(boolean isBuy) {
        if (mPresenter.getSelCoinPriceBean().otcEasyPayFees != null && mPresenter.getSelCoinPriceBean().otcEasyPayFees.size() > 0) {
            List<AdvertCoinPriceBean.OtccEasyPayFeesData> otccEasyPayFeesData = mPresenter.getSelCoinPriceBean().otcEasyPayFees;
            for (int i = 0; i < otccEasyPayFeesData.size(); i++) {

                if (otccEasyPayFeesData.get(i).paymentType == 1) {//微信
                    if (otccEasyPayFeesData.get(i).isShowFee()) {//收手续费
                        tvWeichatFee.setText(getString(R.string.weichat_fee,
                                isBuy ? StringUtil.handleZero((otccEasyPayFeesData.get(i).toRate.doubleValue() * 100) + "") + "%"
                                        : StringUtil.handleZero((otccEasyPayFeesData.get(i).fromRate.doubleValue() * 100) + "") + "%",
                                isBuy ? StringUtil.handleZero(otccEasyPayFeesData.get(i).toBaseFee + "")
                                        : StringUtil.handleZero(otccEasyPayFeesData.get(i).fromBaseFee + ""),
                                mPresenter.getSelFiat()));
                        tvWeichatFee.setVisibility(View.VISIBLE);
                    } else {//不收手续费
                        tvWeichatFee.setText("");
                        tvWeichatFee.setVisibility(View.GONE);
                    }
                } else if (otccEasyPayFeesData.get(i).paymentType == 2) {//微信
                    if (otccEasyPayFeesData.get(i).isShowFee()) {//收手续费
                        tvAlipayFee.setText(getString(R.string.zhifubao_fee,
                                isBuy ? StringUtil.handleZero((otccEasyPayFeesData.get(i).toRate.doubleValue() * 100) + "") + "%"
                                        : StringUtil.handleZero((otccEasyPayFeesData.get(i).fromRate.doubleValue() * 100) + "") + "%",
                                isBuy ? StringUtil.handleZero(otccEasyPayFeesData.get(i).toBaseFee + "")
                                        : StringUtil.handleZero(otccEasyPayFeesData.get(i).fromBaseFee + ""),
                                mPresenter.getSelFiat()));
                        tvAlipayFee.setVisibility(View.VISIBLE);
                    } else {//不收手续费
                        tvAlipayFee.setText("");
                        tvAlipayFee.setVisibility(View.GONE);
                    }
                } else if (otccEasyPayFeesData.get(i).paymentType == 3) {//微信
                    if (otccEasyPayFeesData.get(i).isShowFee()) {//收手续费
                        tvBankCardFee.setText(getString(R.string.zhifubao_fee,
                                isBuy ? StringUtil.handleZero((otccEasyPayFeesData.get(i).toRate.doubleValue() * 100) + "") + "%"
                                        : StringUtil.handleZero((otccEasyPayFeesData.get(i).fromRate.doubleValue() * 100) + "") + "%",
                                isBuy ? StringUtil.handleZero(otccEasyPayFeesData.get(i).toBaseFee + "")
                                        : StringUtil.handleZero(otccEasyPayFeesData.get(i).fromBaseFee + ""),
                                mPresenter.getSelFiat()));
                        tvBankCardFee.setVisibility(View.VISIBLE);
                    } else {//不收手续费
                        tvBankCardFee.setText("");
                        tvBankCardFee.setVisibility(View.GONE);
                    }
                }
            }

        } else {
            setViewFeeGone();
        }
    }

    private void setOtcPayFee(boolean isBuy) {
        if (mPresenter.getSelCoinPriceBean().otcPayFees != null && mPresenter.getSelCoinPriceBean().otcPayFees.size() > 0) {
            List<AdvertCoinPriceBean.OtcPayFeesData> otcPayFees = mPresenter.getSelCoinPriceBean().otcPayFees;
            for (int i = 0; i < otcPayFees.size(); i++) {

                if (otcPayFees.get(i).paymentType == 1) {//微信
                    if (otcPayFees.get(i).isShowFee()) {//收手续费
                        tvWeichatFee.setText(getString(R.string.weichat_fee,
                                isBuy ? StringUtil.handleZero((otcPayFees.get(i).toRate.doubleValue() * 100) + "") + "%"
                                        : StringUtil.handleZero((otcPayFees.get(i).fromRate.doubleValue() * 100) + "") + "%",
                                isBuy ? StringUtil.handleZero(otcPayFees.get(i).toBaseFee + "")
                                        : StringUtil.handleZero(otcPayFees.get(i).fromBaseFee + ""),
                                mPresenter.getSelFiat()));
                        tvWeichatFee.setVisibility(View.VISIBLE);
                    } else {//不收手续费
                        tvWeichatFee.setText("");
                        tvWeichatFee.setVisibility(View.GONE);
                    }
                } else if (otcPayFees.get(i).paymentType == 2) {//微信
                    if (otcPayFees.get(i).isShowFee()) {//收手续费
                        tvAlipayFee.setText(getString(R.string.zhifubao_fee,
                                isBuy ? StringUtil.handleZero((otcPayFees.get(i).toRate.doubleValue() * 100) + "") + "%"
                                        : StringUtil.handleZero((otcPayFees.get(i).fromRate.doubleValue() * 100) + "") + "%",
                                isBuy ? StringUtil.handleZero(otcPayFees.get(i).toBaseFee + "")
                                        : StringUtil.handleZero(otcPayFees.get(i).fromBaseFee + ""),
                                mPresenter.getSelFiat()));
                        tvAlipayFee.setVisibility(View.VISIBLE);
                    } else {//不收手续费
                        tvAlipayFee.setText("");
                        tvAlipayFee.setVisibility(View.GONE);
                    }
                } else if (otcPayFees.get(i).paymentType == 3) {//微信
                    if (otcPayFees.get(i).isShowFee()) {//收手续费
                        tvBankCardFee.setText(getString(R.string.zhifubao_fee,
                                isBuy ? StringUtil.handleZero((otcPayFees.get(i).toRate.doubleValue() * 100) + "") + "%"
                                        : StringUtil.handleZero((otcPayFees.get(i).fromRate.doubleValue() * 100) + "") + "%",
                                isBuy ? StringUtil.handleZero(otcPayFees.get(i).toBaseFee + "")
                                        : StringUtil.handleZero(otcPayFees.get(i).fromBaseFee + ""),
                                mPresenter.getSelFiat()));
                        tvBankCardFee.setVisibility(View.VISIBLE);
                    } else {//不收手续费
                        tvBankCardFee.setText("");
                        tvBankCardFee.setVisibility(View.GONE);
                    }
                }
            }

        } else {
            setViewFeeGone();
        }
    }

    private void setViewFeeGone() {
        tvWeichatFee.setText("");
        tvWeichatFee.setVisibility(View.GONE);
        tvAlipayFee.setText("");
        tvAlipayFee.setVisibility(View.GONE);
        tvBankCardFee.setText("");
        tvBankCardFee.setVisibility(View.GONE);
    }

    @OnClick({R.id.tv_coin, R.id.tv_fiats, R.id.tv_tracking_platform, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_coin:
                onSelectCoin();
                break;
            case R.id.tv_fiats:
                onSelectFiats();
                break;
            case R.id.tv_tracking_platform:
                onSelectTrackingPlatform();
                break;
            case R.id.tv_confirm:
                onConfirm();
                break;
        }
    }

    private void onConfirm() {
        //是否是浮动定价广告
        if (mCbFloatingPricing.getVisibility() == View.VISIBLE && mCbFloatingPricing.isChecked()) {
            publishFloatAdvert();
        } else {
            if (UserHelper.isNonInductivePaySilver()) {
                publishEasyAdvert();
            } else {
                publishNorAdvert();
            }
        }
    }

    /**
     * 发布浮动定价广告
     */
    private void publishFloatAdvert() {

        String selTrackingPlatForm = mPresenter.getSelTrackingPlatForm();
        if (TextUtils.isEmpty(selTrackingPlatForm)) {
            showToast(getString(R.string.please_select_the_platform));
            return;
        }

        double floatRate = new BigDecimal(mEtFloatingRate.getText().toString().trim()).divide(new BigDecimal(100)).doubleValue();

        if (floatRate != 0 && !mCbFloatingRateMode.isChecked()) {
            floatRate = -floatRate;
        }

        String lowPriceStr = mEtLowPrice.getText().toString().trim();
        if (TextUtils.isEmpty(lowPriceStr)) {
            showToast(getString(R.string.please_input_low_price));
            mEtLowPrice.requestFocus();
            return;
        }

        String numsStr = mEtNums.getText().toString().trim();
        if (TextUtils.isEmpty(numsStr)) {
            showToast(getString(R.string.please_input_nums));
            mEtNums.requestFocus();
            return;
        }

        double nums = Double.valueOf(numsStr);
        if (nums == 0) {
            showToast(getString(R.string.ad_coin_nums_not_is_zero));
            mEtNums.requestFocus();
            return;
        }

        //最大最小限额
        String minQuotaStr = mEtMinQuota.getText().toString().trim();
        if (TextUtils.isEmpty(minQuotaStr)) {
            showToast(getString(R.string.please_input_min_quota));
            mEtMinQuota.requestFocus();
            return;
        }
        int minQuota = Integer.valueOf(minQuotaStr);
        if (minQuota == 0) {
            showToast(getString(R.string.min_quota_is_not_zero));
            mEtMinQuota.requestFocus();
            return;
        }

        String maxQuotaStr = mEtMaxQuota.getText().toString().trim();
        if (TextUtils.isEmpty(maxQuotaStr)) {
            showToast(getString(R.string.please_input_max_quota));
            mEtMaxQuota.requestFocus();
            return;
        }
        int maxQuota = Integer.valueOf(maxQuotaStr);
        if (maxQuota == 0) {
            showToast(getString(R.string.max_quota_is_not_zero));
            mEtMaxQuota.requestFocus();
            return;
        }

        if (minQuota > maxQuota) {
            showToast(getString(R.string.the_minimum_limit_should_be_less_than_the_maximum_limit));
            return;
        }

        //数量乘以浮动价格小于最小限额，不能发布广告
        BigDecimal singlePriceBigDecimal = getValuationBigDecimal();
        BigDecimal numsBigDecimal = new BigDecimal(numsStr);
        BigDecimal minQuotaBigDecimal = new BigDecimal(minQuotaStr);

        if (singlePriceBigDecimal.multiply(numsBigDecimal).multiply(minQuotaBigDecimal).doubleValue() < 0) {
            showToast(getString(R.string.quantity_times_floating_price_should_be_greater_than_the_minimum_limit));
            return;
        }


        List<BankCardBean> items = mPresenter.getTempPaymentModes();
        if (items.size() == 0) {//表示用户当前还没有对应法币的收款方式，引导其前往设置收款方式
            onNotSetCollectionMode(getString(R.string.please_add_the_collection_method_of_this_legal_currency));
            return;
        }

        StringBuilder sb = new StringBuilder();
        HashSet<Integer> bankTypeSet = new HashSet<>();
        for (BankCardBean item : items) {
            if (item.isSelected()) {
                sb.append(item.getId()).append(",");
                bankTypeSet.add(item.getType());
            }
        }

        String supportBank = sb.toString();
        if (TextUtils.isEmpty(supportBank)) {
            showToast(getString(R.string.please_select_pay_mode));
            return;
        }

//        if (bankTypeSet.size() < 2) {
//            showToast(getString(R.string.choose_at_least_two_types_of_collection));
//            return;
//        }

        boolean isBuy = mRbBuy.isChecked();

        if (TextUtils.isEmpty(mPresenter.getSelCoin())) {
            showToast(ResourceUtil.getString(R.string.get_coin_info_fail_and_retry_get));
            mPresenter.requestCoinTypeInfo(true);
            return;
        }

        if (TextUtils.isEmpty(mPresenter.getSelFiat())) {
            mPresenter.requestBankCardList(true);
            return;
        }
        mTvConfirm.setEnabled(false);
        mPresenter.publishFloatAdvert(numsStr, isBuy, supportBank.substring(0, supportBank.length() - 1),
                mPresenter.getCurAdvertPreBean().getPlatId(), floatRate, lowPriceStr, minQuota, maxQuota);
    }

    /**
     * @return 提交时判断单价是否合法
     */
    public boolean isUnitPriceLegal(String singlePriceStr) {
        //是否有输入单价

        if (TextUtils.isEmpty(singlePriceStr)) {
            showToast(getString(R.string.please_input_single_price));
            mEtUnitPrice.requestFocus();
            return false;
        }
        //输入的单价是否为0
        double singlePrice = Double.valueOf(singlePriceStr);
        if (singlePrice == 0) {
            showToast(getString(R.string.ad_single_price_not_is_zero));
            mEtUnitPrice.requestFocus();
            return false;
        }

        //判断输入的单价是否在范围内
        AdvertCoinPriceBean selCoinPriceBean = mPresenter.getSelCoinPriceBean();//当前交易对的单价范围对象
        if (selCoinPriceBean == null) {
            showToast(getString(R.string.failed_to_get_unit_price_limit_information_for_current_trade_pair));
            return false;
        }

        boolean isBuy = mRbBuy.isChecked();
        BigDecimal singlePriceDecimal = new BigDecimal(singlePriceStr);
        BigDecimal minPriceDecimal = new BigDecimal(isBuy ? selCoinPriceBean.getBuyMinPrice() : selCoinPriceBean.getSellMinPrice());
        if (singlePriceDecimal.doubleValue() < minPriceDecimal.doubleValue()) {
            mEtUnitPrice.requestFocus();
            showToast(String.format(getString(R.string.advert_single_price_limit_format), mEtUnitPrice.getHint().toString()));
            return false;
        }

        BigDecimal maxPriceDecimal = new BigDecimal(isBuy ? selCoinPriceBean.getBuyMaxPrice() : selCoinPriceBean.getSellMaxPrice());
        if (singlePriceDecimal.doubleValue() > maxPriceDecimal.doubleValue()) {
            mEtUnitPrice.requestFocus();
            showToast(String.format(getString(R.string.advert_single_price_limit_format), mEtUnitPrice.getHint().toString()));
            return false;
        }
        return true;
    }

    /**
     * 发布无感支付广告
     */
    private void publishEasyAdvert() {
        String singlePriceStr = mEtUnitPrice.getText().toString().trim();
        String numsStr = mEtNums.getText().toString().trim();
        if (TextUtils.isEmpty(numsStr)) {
            showToast(getString(R.string.please_input_nums));
            mEtNums.requestFocus();
            return;
        }

        double nums = Double.valueOf(numsStr);
        if (nums == 0) {
            showToast(getString(R.string.ad_coin_nums_not_is_zero));
            mEtNums.requestFocus();
            return;
        }

        //最大最小限额
        String minQuotaStr = mEtMinQuota.getText().toString().trim();
        if (TextUtils.isEmpty(minQuotaStr)) {
            showToast(getString(R.string.please_input_min_quota));
            mEtMinQuota.requestFocus();
            return;
        }
        int minQuota = Integer.valueOf(minQuotaStr);
        if (minQuota == 0) {
            showToast(getString(R.string.min_quota_is_not_zero));
            mEtMinQuota.requestFocus();
            return;
        }

        String maxQuotaStr = mEtMaxQuota.getText().toString().trim();
        if (TextUtils.isEmpty(maxQuotaStr)) {
            showToast(getString(R.string.please_input_max_quota));
            mEtMaxQuota.requestFocus();
            return;
        }
        int maxQuota = Integer.valueOf(maxQuotaStr);
        if (maxQuota == 0) {
            showToast(getString(R.string.max_quota_is_not_zero));
            mEtMaxQuota.requestFocus();
            return;
        }

        if (minQuota > maxQuota) {
            showToast(getString(R.string.the_minimum_limit_should_be_less_than_the_maximum_limit));
            return;
        }
        //数量乘以单价小于最小限额，不能发布广告
        BigDecimal singlePriceBigDecimal = new BigDecimal(singlePriceStr);
        BigDecimal numsBigDecimal = new BigDecimal(numsStr);
        BigDecimal minQuotaBigDecimal = new BigDecimal(minQuotaStr);

        if (singlePriceBigDecimal.multiply(numsBigDecimal).multiply(minQuotaBigDecimal).doubleValue() < 0) {
            showToast(getString(R.string.quantity_times_unit_price_should_be_greater_than_the_minimum_limit));
            return;
        }

        List<BankCardBean> items = mPresenter.getTempPaymentModes();
        if (items.size() == 0) {//表示用户当前还没有对应法币的收款方式，引导其前往设置收款方式
            onNotSetCollectionMode(getString(R.string.please_add_the_collection_method_of_this_legal_currency));
            return;
        }

        StringBuilder sb = new StringBuilder();
        HashSet<Integer> bankTypeSet = new HashSet<>();
        for (BankCardBean item : items) {
            if (item.isSelected()) {
                sb.append(item.getId()).append(",");
                bankTypeSet.add(item.getType());
            }
        }

        String supportBank = sb.toString();

        if (TextUtils.isEmpty(supportBank)) {
            showToast(getString(R.string.please_select_pay_mode));
            return;
        }

//        if (bankTypeSet.size() < 2) {
//            showToast(getString(R.string.choose_at_least_two_types_of_collection));
//            return;
//        }

        boolean isBuy = mRbBuy.isChecked();

        if (TextUtils.isEmpty(mPresenter.getSelCoin())) {
            showToast(ResourceUtil.getString(R.string.get_coin_info_fail_and_retry_get));
            mPresenter.requestCoinTypeInfo(true);
            return;
        }

        if (TextUtils.isEmpty(mPresenter.getSelFiat())) {
            mPresenter.requestBankCardList(true);
            return;
        }
        mTvConfirm.setEnabled(false);
        mPresenter.publishEasyAdvert(singlePriceStr, numsStr, isBuy, supportBank.substring(0, supportBank.length() - 1), minQuota, maxQuota);
    }

    /**
     * 发布普通广告
     */
    private void publishNorAdvert() {
        String singlePriceStr = mEtUnitPrice.getText().toString().trim();
        if (!isUnitPriceLegal(singlePriceStr)) {
            return;
        }

        String numsStr = mEtNums.getText().toString().trim();
        if (TextUtils.isEmpty(numsStr)) {
            showToast(getString(R.string.please_input_nums));
            mEtNums.requestFocus();
            return;
        }

        double nums = Double.valueOf(numsStr);
        if (nums == 0) {
            showToast(getString(R.string.ad_coin_nums_not_is_zero));
            mEtNums.requestFocus();
            return;
        }

        //最大最小限额
        String minQuotaStr = mEtMinQuota.getText().toString().trim();
        if (TextUtils.isEmpty(minQuotaStr)) {
            showToast(getString(R.string.please_input_min_quota));
            mEtMinQuota.requestFocus();
            return;
        }
        int minQuota = Integer.valueOf(minQuotaStr);
        if (minQuota == 0) {
            showToast(getString(R.string.min_quota_is_not_zero));
            mEtMinQuota.requestFocus();
            return;
        }

        String maxQuotaStr = mEtMaxQuota.getText().toString().trim();
        if (TextUtils.isEmpty(maxQuotaStr)) {
            showToast(getString(R.string.please_input_max_quota));
            mEtMaxQuota.requestFocus();
            return;
        }
        int maxQuota = Integer.valueOf(maxQuotaStr);
        if (maxQuota == 0) {
            showToast(getString(R.string.max_quota_is_not_zero));
            mEtMaxQuota.requestFocus();
            return;
        }

        if (minQuota > maxQuota) {
            showToast(getString(R.string.the_minimum_limit_should_be_less_than_the_maximum_limit));
            return;
        }
        //数量乘以单价小于最小限额，不能发布广告
        BigDecimal singlePriceBigDecimal = new BigDecimal(singlePriceStr);
        BigDecimal numsBigDecimal = new BigDecimal(numsStr);
        BigDecimal minQuotaBigDecimal = new BigDecimal(minQuotaStr);

        if (singlePriceBigDecimal.multiply(numsBigDecimal).multiply(minQuotaBigDecimal).doubleValue() < 0) {
            showToast(getString(R.string.quantity_times_unit_price_should_be_greater_than_the_minimum_limit));
            return;
        }

        List<BankCardBean> items = mPresenter.getTempPaymentModes();
        if (items.size() == 0) {//表示用户当前还没有对应法币的收款方式，引导其前往设置收款方式
            onNotSetCollectionMode(getString(R.string.please_add_the_collection_method_of_this_legal_currency));
            return;
        }

        StringBuilder sb = new StringBuilder();
        HashSet<Integer> bankTypeSet = new HashSet<>();
        for (BankCardBean item : items) {
            if (item.isSelected()) {
                sb.append(item.getId()).append(",");
                bankTypeSet.add(item.getType());
            }
        }

        String supportBank = sb.toString();

        if (TextUtils.isEmpty(supportBank)) {
            showToast(getString(R.string.please_select_pay_mode));
            return;
        }

//        if (bankTypeSet.size() < 2) {
//            showToast(getString(R.string.choose_at_least_two_types_of_collection));
//            return;
//        }

        boolean isBuy = mRbBuy.isChecked();

        if (TextUtils.isEmpty(mPresenter.getSelCoin())) {
            showToast(ResourceUtil.getString(R.string.get_coin_info_fail_and_retry_get));
            mPresenter.requestCoinTypeInfo(true);
            return;
        }

        if (TextUtils.isEmpty(mPresenter.getSelFiat())) {
            mPresenter.requestBankCardList(true);
            return;
        }
        mTvConfirm.setEnabled(false);
        mPresenter.publishAdvert(singlePriceStr, numsStr, isBuy, supportBank.substring(0, supportBank.length() - 1), minQuota, maxQuota);
    }

    private void onSelectCoin() {

        if (TextUtils.isEmpty(mPresenter.getSelCoin())) {
            showToast(ResourceUtil.getString(R.string.get_coin_info_fail_and_retry_get));
            mPresenter.requestCoinTypeInfo(true);
            return;
        }

        if (mSelectCoinPopupWindow == null) {
            mSelectCoinPopupWindow = new AdvertFiatPopupWindow(this);
            mSelectCoinPopupWindow
                    .setOnSelFiatListener(new AdvertFiatPopupWindow.OnSelFiatListener() {
                        @Override
                        public void onSelFiat(String coin) {
                            updateCoin(coin);
                        }
                    })
                    .setWidth(mTvCoin.getWidth())
                    .gravity(Gravity.BOTTOM)
                    .showAnim(new CustomBounceTopEnter())
                    .dismissAnim(new SlideTopExit())
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            if (mTvCoin != null) {
                                mTvCoin.setSelected(false);
                            }
                        }
                    });
        }
        mSelectCoinPopupWindow.offset(0, 0)
                .anchorView(mTvCoin)
                .setData(mPresenter.getCoins())
                .setSelItem(mPresenter.getSelCoin()).show();
        mTvCoin.setSelected(true);
    }

    private void onSelectFiats() {

        if (TextUtils.isEmpty(mPresenter.getSelFiat())) {
            mPresenter.requestBankCardList(true);
            return;
        }

        if (mSelectFiatPopupWindow == null) {
            mSelectFiatPopupWindow = new AdvertFiatPopupWindow(this);
            mSelectFiatPopupWindow
                    .setOnSelFiatListener(new AdvertFiatPopupWindow.OnSelFiatListener() {
                        @Override
                        public void onSelFiat(String fiat) {
                            updateFiat(fiat);
                            updateAllFee();//更新手续费
                        }
                    })
                    .setWidth(mTvFiats.getWidth())
                    .gravity(Gravity.BOTTOM)
                    .showAnim(new CustomBounceTopEnter())
                    .dismissAnim(new SlideTopExit())
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            if (mTvFiats != null) {
                                mTvFiats.setSelected(false);
                            }
                        }
                    });
        }
        mSelectFiatPopupWindow.offset(0, 0)
                .anchorView(mTvFiats)
                .setData(mPresenter.getFiats())
                .setSelItem(mPresenter.getSelFiat()).show();
        mTvFiats.setSelected(true);
    }

    /**
     * 显示选择平台列表
     */
    private void onSelectTrackingPlatform() {
        if (mSelectTrackingPlatformPopupWindow == null) {
            mSelectTrackingPlatformPopupWindow = new AdvertFiatPopupWindow(this);
            mSelectTrackingPlatformPopupWindow
                    .setOnSelFiatListener(new AdvertFiatPopupWindow.OnSelFiatListener() {
                        @Override
                        public void onSelFiat(String trackingPlatForm) {
                            mPresenter.setSelTrackingPlatForm(trackingPlatForm);
                            mTvTrackingPlatform.setText(mPresenter.getSelTrackingPlatForm());
                            updateValuation();//更新估值
                        }
                    })
                    .setWidth(mTvTrackingPlatform.getWidth())
                    .gravity(Gravity.BOTTOM)
                    .showAnim(new CustomBounceTopEnter())
                    .dismissAnim(new SlideTopExit())
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            if (mTvTrackingPlatform != null) {
                                mTvTrackingPlatform.setSelected(false);
                            }
                        }
                    });
        }
        mSelectTrackingPlatformPopupWindow.offset(0, 0)
                .anchorView(mTvTrackingPlatform)
                .setData(mPresenter.getTrackingPlatForm())
                .setSelItem(mPresenter.getSelTrackingPlatForm()).show();
        mTvTrackingPlatform.setSelected(true);
    }


    private void updateCoin(String coin) {
        mTvCoin.setText(coin);
        mTvUnitNums.setText(coin);
        mPresenter.setSelCoin(coin);

        updateEtNumsHint();
//        updateFee();//更新手续费
        updateAllFee();//更新手续费
    }

    private void updateEtNumsHint() {
//        AdvertCoinTypeBean selCoinTypeBeans = mPresenter.getSelCoinTypeBeans();
//        mEtNums.setHint(getString(R.string.publish_advert_num_section, StringUtil.handleZero(selCoinTypeBeans.getMinVolume()), StringUtil.handleZero(selCoinTypeBeans.getMaxVolume())));
    }

    @Override
    public void updateFiat(String fiat) {
        mTvFiats.setText(fiat);
        mTvUnitPriceUnit.setText(fiat);
        mPresenter.setSelFiat(fiat);
        mTvQuotaUnit.setText(fiat);

        //更新追踪平台信息
        updateTrackingPlatformInfo();
        //更新收款方式
        updatePaymentMode(fiat);
        //如果无感支付，需要设置单价
        updateNonInductiveUnitPrice();


    }

    /**
     * 设置无感支付单价
     */
    private void updateNonInductiveUnitPrice() {
        if (!UserHelper.isNonInductivePaySilver()) return;
        AdvertCoinPriceBean selCoinPriceBean = mPresenter.getSelCoinPriceBean();
        if (selCoinPriceBean == null) return;
        boolean isBuy = mRgDealType.getCheckedRadioButtonId() == R.id.rb_buy;
        mEtUnitPrice.setText(StringUtil.handleZero(isBuy ? selCoinPriceBean.getEasyBuyPrice() : selCoinPriceBean.getEasySellPrice()));
    }

    @Override
    public void updateTrackingPlatformInfo() {
        mPresenter.updateTrackingPlatFormMaps(mPresenter.getCurTrackingPlatformInfo());
        //当无感支付广告商时，不展示浮动定价信息，单价固定且不可以修改
        if (UserHelper.isNonInductivePaySilver()) {
            mPresenter.setSelTrackingPlatForm("");
            mCbFloatingPricing.setVisibility(View.GONE);
            mClVariablePricing.setVisibility(View.GONE);
            mEtUnitPrice.setEnabled(false);
            mTvUnitPriceUnit.setEnabled(false);
            mEtUnitPrice.setHint("");
            return;
        }

        ArrayList<String> trackingPlatForm = mPresenter.getTrackingPlatForm();

        if (trackingPlatForm.size() == 0) {//当前没有追踪平台
            mPresenter.setSelTrackingPlatForm("");
            mCbFloatingPricing.setVisibility(View.GONE);
            mClVariablePricing.setVisibility(View.GONE);
            mEtUnitPrice.setEnabled(true);
            mTvUnitPriceUnit.setEnabled(true);
            mEtUnitPrice.setHint(getNorSinglePriceDesc());
        } else {//当前有追踪平台
            String selTrackingPlatForm = mPresenter.getSelTrackingPlatForm();
            if (TextUtils.isEmpty(selTrackingPlatForm) || !trackingPlatForm.contains(selTrackingPlatForm)) {
                mPresenter.setSelTrackingPlatForm(trackingPlatForm.get(0));
            }

            selTrackingPlatForm = mPresenter.getSelTrackingPlatForm();
            mPresenter.setSelTrackingPlatForm(selTrackingPlatForm);
            mTvTrackingPlatform.setText(selTrackingPlatForm);
            mCbFloatingPricing.setVisibility(View.VISIBLE);
            boolean floatingPricingChecked = mCbFloatingPricing.isChecked();
            mClVariablePricing.setVisibility(floatingPricingChecked ? View.VISIBLE : View.GONE);
            mEtUnitPrice.setEnabled(!floatingPricingChecked);
            mTvUnitPriceUnit.setEnabled(!floatingPricingChecked);
            updateValuation();//更新估值
            mEtUnitPrice.setHint(mCbFloatingPricing.isChecked() ? getString(R.string.please_set_the_floating_price) : getNorSinglePriceDesc());
        }
    }

    /**
     * 更新估值
     */
    private void updateValuation() {
        mTvValuation.setText(String.format("%s %s", StringUtil.formatNumber(getValuationBigDecimal().doubleValue(), 2, false), mPresenter.getSelFiat()));
    }

    public BigDecimal getValuationBigDecimal() {
        String floatingRateStr = mEtFloatingRate.getText().toString().trim();

        AdvertPreBean curAdvertPreBean = mPresenter.getCurAdvertPreBean();
        if (curAdvertPreBean == null) {
            return new BigDecimal(0);
        }
        String priceStr = curAdvertPreBean.getPrice();
        if (TextUtils.isEmpty(priceStr)) {
            return new BigDecimal(0);
        }

        BigDecimal floatingRate = new BigDecimal(floatingRateStr).divide(new BigDecimal(100));
        BigDecimal price = new BigDecimal(priceStr);
        boolean isFloatUpward = mCbFloatingRateMode.isChecked();

        if (isFloatUpward) {
            return new BigDecimal(1).add(floatingRate).multiply(price);
        } else {
            return new BigDecimal(1).subtract(floatingRate).multiply(price);
        }
    }

    @Override
    public void updatePaymentMode(String fiat) {
        ArrayList<BankCardBean> cardBeans = mPresenter.getTradeModesByFiat(fiat);
        ArrayList<BankCardBean> temp = new ArrayList<>();

        //进行收款方式排序，优先展示微信、支付宝，然后再展示银行卡
        if (cardBeans != null) {
            BankCardBean alipay = null;
            BankCardBean weixin = null;
            for (BankCardBean cardBean : cardBeans) {
                cardBean.setSelected(false);
                if (BankType.ALIPAY.equals(cardBean.getBankType())) {
                    alipay = cardBean;
                } else if (BankType.WEXIN.equals(cardBean.getBankType())) {
                    weixin = cardBean;
                } else {
                    temp.add(cardBean);
                }
            }
            if (weixin != null) {
                temp.add(0, weixin);
            }
            if (alipay != null) {
                temp.add(0, alipay);
            }
        }
        //缓存当前法币对应收款方式集合
        mPresenter.setTempPaymentMode(temp);
        //更新收款方式ui
        initPaymentModeUI();

    }

    /**
     * @return 当前交易对的单价限制描述
     */
    public String getNorSinglePriceDesc() {
        AdvertCoinPriceBean selCoinPriceBean = mPresenter.getSelCoinPriceBean();
        if (selCoinPriceBean == null) {
            return getString(R.string.please_input_single_price);
        }

        boolean isBuy = mRgDealType.getCheckedRadioButtonId() == R.id.rb_buy;
        return ResourceUtil.getString(R.string.advert_single_price_range,
                StringUtil.handleZero(isBuy ? selCoinPriceBean.getBuyMinPrice() : selCoinPriceBean.getSellMinPrice()),
                StringUtil.handleZero(isBuy ? selCoinPriceBean.getBuyMaxPrice() : selCoinPriceBean.getSellMaxPrice()));
    }

    /**
     * 更新收款方式ui
     */
    private void initPaymentModeUI() {
        mFlexboxLayout.removeAllViews();
        for (final BankCardBean cardBean : mPresenter.getTempPaymentModes()) {
            final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_pay_mode, null);
            TextView tvPayMode = view.findViewById(R.id.tv_pay_mode);
            tvPayMode.setText(cardBean.getBankName());
            int width = DataUtils.dp2px(this, 55) + DataUtils.dp2px(this, 14) * cardBean.getBankName().length();
            FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
            mFlexboxLayout.addView(view, layoutParams);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (BankType.ALIPAY.equals(cardBean.getBankType()) || BankType.WEXIN.equals(cardBean.getBankType())) {
                        boolean selected = view.isSelected();
                        cardBean.setSelected(!selected);
                        view.setSelected(!selected);
                        view.findViewById(R.id.iv_checked).setVisibility(!selected ? View.VISIBLE : View.GONE);
                    } else {

/*//                       收款方式多选
                        boolean selected = view.isSelected();
                        cardBean.setSelected(!selected);
                        view.setSelected(!selected);
                        view.findViewById(R.id.iv_checked).setVisibility(!selected ? View.VISIBLE : View.GONE);

//                     */

// 收款方式单选
                        if (view.isSelected()) {
                            cardBean.setSelected(false);
                            view.setSelected(false);
                            view.findViewById(R.id.iv_checked).setVisibility(cardBean.isSelected() ? View.VISIBLE : View.GONE);
                            return;
                        }

                        ArrayList<BankCardBean> tempPaymentModes = mPresenter.getTempPaymentModes();
                        for (int i = 0; i < tempPaymentModes.size(); i++) {
                            BankCardBean teamPaymentMode = tempPaymentModes.get(i);
                            if (BankType.BANK.equals(teamPaymentMode.getBankType())) {
                                teamPaymentMode.setSelected(cardBean.equals(teamPaymentMode));
                                View child = mFlexboxLayout.getChildAt(i);
                                child.setSelected(teamPaymentMode.isSelected());
                                child.findViewById(R.id.iv_checked).setVisibility(teamPaymentMode.isSelected() ? View.VISIBLE : View.GONE);
                            }
                        }
                    }
                    //当为无感广告商且在选中收款方式时才进行关联金额提示
                    if (UserHelper.isNonInductivePaySilver() && cardBean.isSelected()) {
                        ArrayList<AdRelatedAmountBean> items = cardBean.getItems();
                        if (items == null || items.size() == 0) return;
                        String selFiat = mPresenter.getSelFiat();
                        if (TextUtils.isEmpty(selFiat)) return;
                        for (AdRelatedAmountBean item : items) {
                            if (selFiat.equals(item.getLegal())) {
                                showToast(String.format(getString(R.string.this_card_has_been_associated_with_the_amount_of_advertising),
                                        StringUtil.formatNumber(item.getPayVolume(), 2, RoundingMode.DOWN, true), selFiat));
                                break;
                            }
                        }
                    }
                }
            });
        }
    }


    @Override
    public void publishAdvertSuccess(boolean isBuy) {
        showToast(getString(R.string.publish_success));
        mTvConfirm.setEnabled(true);
        post(new Event.OnPublishAdvertEvent(isBuy ? TradeType.BUY : TradeType.SALE));
        finish();
    }

    @Override
    public void publishAdvertFail(String errMessage) {
        mTvConfirm.setEnabled(true);
        showToast(errMessage);
    }

    @Override
    public void requestCoinTypeInfoSuccess(ArrayList<AdvertCoinTypeBean> coinTypeBeans) {

        if (coinTypeBeans == null || coinTypeBeans.size() == 0) {
            return;
        }

        mPresenter.setCoinTypes(coinTypeBeans);
        updateCoin(mPresenter.getCoins().get(0));
    }

    @Override
    public void requestBankCardListSuccess(ArrayList<BankCardBean> bankCardBeans) {
        if (bankCardBeans == null || bankCardBeans.size() == 0) {
            //提示还没有设置收款方式
            onNotSetCollectionMode(ResourceUtil.getString(R.string.not_set_the_payment_collection_mode));
            return;
        }
        mPresenter.setTradeModes(bankCardBeans);
        if (TextUtils.isEmpty(mPresenter.getSelFiat())) {
            updatePaymentMode(mPresenter.getSelFiat());
        }
    }

    /**
     * 还未设置收款方式
     */
    public void onNotSetCollectionMode(String errMessage) {
        if (mMultipleDialog == null) {
            mMultipleDialog = new MultipleBlocksDialog(this);
        }
        mMultipleDialog.setTitle(getString(R.string.hint)).setContent(errMessage);
        mMultipleDialog.show();
        mMultipleDialog.setOnDialogItemClickListener(new MultipleBlocksDialog.OnDialogItemClickListener() {
            @Override
            public void onSure() {
                $startActivityForResult(BankCardActivity.class, REQUEST_CODE_SET_COLLECTION_MODE);
            }

            @Override
            public void onCancel() {
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SET_COLLECTION_MODE) {
            mPresenter.requestBankCardList(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.dismissDialog(mSelectCoinPopupWindow);
        UIUtils.dismissDialog(mSelectFiatPopupWindow);
        UIUtils.dismissDialog(mMultipleDialog);
    }
}
